package com.dreampharos.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;


/**
 * Copyright 2005 ICOS.CO.KR, Inc. All Rights Reserved.
 * Description:
 *
 * @author �����
 * @version 1.0
 * @since 2005. 7. 12.
 * Revision:
 */
public class FileDown extends HttpServlet {
    private Logger logger = Logger.getLogger(this.getClass());

     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("EUC-KR");
        doGet(request, response);         
     }


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
        String clips = request.getParameter("file_save_name");
        String path  = request.getParameter("file_save_path");
        String grade = StringUtil.nullChk(request.getParameter("file_grade"));
    	System.out.println("grade : "+grade);  
//        String path = getServletContext().getRealPath("/WEB-INF");
//        logger.debug(" path : " + path);
//        String other_path = "/upload/";
//        logger.debug(" file name : " + clips);
        String filename = clips; //���� �̸��� �޴´�.
        
        //����
		HttpSession session     = null;		
		
		session = request.getSession(true) ; 
		String loginTF = (String)session.getAttribute("Login");
		
		if("".equals(grade)) {
			if(loginTF == null){
				/* �α����� �Ǵ� �����̳� ������ ��쿡�� ������ �޴µ� ������ ������
				 * �Ϲ� ����ڰ� ������ �ٿ� �޴� ��� �α����� ���� �ʱ� ������ �α��� ���� üũ�� ������ �ȴ�.  */			
				RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/login.do");
				dispatcher.forward(request, response); 
			}else if(loginTF.equals("true")){
	        
		//        FileUtil fileUtil = new FileUtil(path + other_path);
		        int cnt = -1;
		        /*try {
		            cnt = fileUtil.GetFile(file_key, filename);
		        } catch (IMSException ie) {
		            logger.error(ie);
		        }*/
		
		        /*if (cnt < 0) {
		            throw new IOException("file not found.");
		        } else {*/
		
		            File file = new File( path + filename); // ������Դϴ�.
		            logger.debug(" full path : " + path + filename);
		            
		            //�� �κ��� ����� ������ ��.
		
		            setHeader(response, file);
		            transport(new FileInputStream(file), response.getOutputStream());
		//        }
			}
		}else {
			int cnt = -1;
			File file = new File( path + filename); // ������Դϴ�.
			
			 setHeader(response, file);
	         transport(new FileInputStream(file), response.getOutputStream());
		}
    }

    /**
     * Method setHeader.
     *
     * @param response
     * @param file
     */
    private void setHeader(HttpServletResponse response, File file) {
        String mime = getServletContext().getMimeType(file.toString());
        System.out.println("mime : " + mime + "   :   " + I18nUtil.K2E(file.getName()));
        if (mime == null) mime = "application/octet-stream";
        response.setContentType(mime);

        response.setHeader("Content-Disposition",
                "attachment; filename=" + I18nUtil.K2E(file.getName()) + ";");
        response.setHeader("Content-Length", "" + file.length());
    }

    /**
     * Method transport.
     *
     * @param in
     * @param out
     * @throws IOException
     */
    private void transport(InputStream in, OutputStream out)
            throws IOException {

        BufferedInputStream bin  = null;
        BufferedOutputStream bos = null;

        try {
            bin = new BufferedInputStream(in);
            bos = new BufferedOutputStream(out);

            byte[] buf = new byte[2048]; //buffer size 2K.
            int read = 0;
            while ((read = bin.read(buf)) != -1) {
                bos.write(buf, 0, read);
            }
        } finally {
            bos.close();
            bin.close();
        }
    }

}